package com.shawn.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shawn.commons.utils.EducationTransfer;
import com.shawn.commons.utils.RC;
import com.shawn.dubbo.mapper.SettingsMapper;
import com.shawn.dubbo.mapper.UserInfoMapper;
import com.shawn.model.entity.*;
import com.shawn.model.vo.DistributionVo;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.UserInfoVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void saveUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findDetailById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public PageResult<UserInfo> findByIds(List<Long> userIds, UserInfo userInfo, Integer page, Integer pageSize) {
        //创建分页
        Page<UserInfo> userInfoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        //批量查询id
        wrapper.in(UserInfo::getId, userIds);
        //性别相同
        wrapper.eq(!Strings.isEmpty(userInfo.getGender()), UserInfo::getGender, userInfo.getGender());
        //年龄小于
        wrapper.le(userInfo.getAge() != null, UserInfo::getAge, userInfo.getAge());
        //城市模糊匹配
        wrapper.like(!Strings.isEmpty(userInfo.getCity()), UserInfo::getCity, userInfo.getCity());
        //学历向上兼容
        if (!Strings.isEmpty(userInfo.getEducation())) {
            ArrayList<String> educations = EducationTransfer.getEducations(userInfo.getEducation());
            wrapper.in(UserInfo::getEducation, educations);
        }
        //姓名模糊查询
        wrapper.like(!Strings.isEmpty(userInfo.getNickname()), UserInfo::getNickname, userInfo.getNickname());
        //根据姓名排序
        //根据姓氏升序排列
        wrapper.orderByAsc(UserInfo::getNickname);
        IPage<UserInfo> infoPage = userInfoMapper.selectPage(userInfoPage, wrapper);
        if (infoPage.getRecords() == null) {
            return null;
        }
        return new PageResult<>(page, pageSize, (int) infoPage.getTotal(), infoPage.getRecords());
    }

    @Override
    public List<UserInfo> findByIds(List<Long> userIds) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId, userIds);
        return userInfoMapper.selectList(wrapper);
    }

    /**
     * 分页查询用户信息
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@link PageResult}<{@link UserInfo}>
     */
    @Override
    public PageResult<UserInfo> selectByPage(Integer page, Integer pageSize,Long id,String nickname,String city) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(id),UserInfo::getId,id);
        wrapper.like(StringUtils.isNotEmpty(nickname),UserInfo::getNickname,nickname);
        wrapper.like(StringUtils.isNotEmpty(city),UserInfo::getCity,city);
        IPage<UserInfo> userInfoIPage = userInfoMapper.selectPage(new Page<>(page, pageSize), wrapper);
        return new PageResult<>(page, pageSize, (int) userInfoIPage.getTotal(), userInfoIPage.getRecords());
    }

    @Override
    public DistributionVo getDistribution(Long startDate, Long endDate, DistributionVo vo) {
        Date start = new Date(startDate);
        Date end = new Date(endDate);

        //获取起始时间段内所有用户
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(UserInfo::getCreated, start).le(UserInfo::getCreated, end);
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);

        List<GenderDistribution> genderDistribution = vo.getGenderDistribution();
        List<AgeDistribution> ageDistribution = vo.getAgeDistribution();
        List<IndustryDistribution> industryDistribution = vo.getIndustryDistribution();
        List<LocalDistribution> localDistribution = vo.getLocalDistribution();
        List<LocalTotal> localTotal = vo.getLocalTotal();
        //遍历所有用户
        for (UserInfo userInfo : userInfos) {
            //遍历所有行业集合
            String profession = userInfo.getProfession();
            for (IndustryDistribution distribution : industryDistribution) {
                if (StringUtils.isEmpty(profession)) {
                    break;
                }
                if (profession.contains(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                }
            }

            //遍历所有年龄分布集合
            Integer age = userInfo.getAge();
            for (AgeDistribution distribution : ageDistribution) {
                if (age < 24 && "18-23岁".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                } else if (age < 31 && "24-30岁".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                } else if (age < 41 && "31-40岁".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                } else if (age <= 50 && "41-50岁".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                } else if (age > 50 && "50岁+".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                }
            }
            //得到所有性别分布
            String gender = userInfo.getGender();
            for (GenderDistribution distribution : genderDistribution) {
                if ("man".equals(gender) && "男性用户".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                } else if ("woman".equals(gender) && "女性用户".equals(distribution.getTitle())) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    break;
                }
            }

            //得到所有地址分布
            String city = userInfo.getCity();
            for (LocalDistribution distribution : localDistribution) {
                String title = distribution.getTitle();
                if (StringUtils.isEmpty(title)){
                    break;
                }
                if (city.contains(title)) {
                    distribution.setAmount(distribution.getAmount() + 1);
                    for (LocalTotal total : localTotal) {
                        if ("上海市、江苏省、浙江省、安徽省、福建省、江西省、山东省、台湾省".contains(title) && "华东地区".equals(total.getTitle())) {
                            total.setAmount(total.getAmount() + 1);
                            break;
                        } else if ("北京市、天津市、山西省、河北省、内蒙古自治区、黑龙江省、吉林省、辽宁省".contains(title) && "华北地区".equals(total.getTitle())) {
                            total.setAmount(total.getAmount() + 1);
                            break;
                        } else if ("河南省、湖北省、湖南省".contains(title) && "华中地区".equals(total.getTitle())) {
                            total.setAmount(total.getAmount() + 1);
                            break;
                        } else if ("广东省、广西壮族自治区、海南省、香港特别行政区、澳门特别行政区".contains(title) && "华南地区".equals(total.getTitle())) {
                            total.setAmount(total.getAmount() + 1);
                            break;
                        } else if ("四川省、贵州省、云南省、重庆市、西藏自治区、陕西省、甘肃省、青海省、宁夏回族自治区、新疆维吾尔自治区".contains(title) && "华西地区".equals(total.getTitle())) {
                            total.setAmount(total.getAmount() + 1);
                            break;
                        }
                    }
                    break;
                }
            }

        }
        return vo;
    }
}
